﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
    public class ActiveObject : IDisposable
    {
        private Task _endTask;

        public ActiveObject()
        {
            var startTask = new Task(() => { });
            startTask.Start();
            this._endTask = startTask;
        }

        public void InvokeAsync(Action action)
        {
            lock (this._endTask)
            {
                this._endTask = this._endTask.ContinueWith(t => action());
            }
        }

        public void Dispose()
        {
            this._endTask.Wait();
            this._endTask.Dispose();
        }
    }
}
